import Vue from 'vue';
import './style.scss';
import tpl from './tpl.html';
import modal from '../../../../../grsc-base/component/Modal.vue';
import help from '../../../../../grsc-base/component/Help.vue';
import Popover from '../../../../../grsc-base/component/Popover.vue';
import service from '../../basic/service';
import Validator from '../../../../../grsc-base/component/Validator/main';
export default Vue.extend({
  template: tpl,
  components: {
    modal,
    Popover,
    help,
    Validator
  },
  props: ['states'],
  data() {
    return {
      payPassWord: '',
      form: {
        paypwd: ''
      },
      rules:{
        paypwd:{
          required: true,
          requiredTip: this.$t('common.assetPassRequireTip')
        }
      },
    }
  },
  created() {},
  methods: {
    //提交开启或者关闭
    submit() {
      // if (!this.form.paypwd) {
      //   alert({
      //     content: this.$t('common.assetPassRequireTip')
      //   })
      //   return false;
      // }
      if (this.rules.$checkAll() === false) {
        return;
      }
      let param = {
        states: this.states == false ? 1 : 0
      }

      let header = {
        'pay-password': this.form.paypwd
      };

      service.UserUpDate(param, header).done((rep) => {
        if (rep.code == '100200') {
          window.success(rep.msg)
          this.hide(rep.data.states == 1 ? true : false);
        } else if (rep.code === window.CODE.PAY_PWD.CODE && rep.data && rep.data.num) {
          let num = rep.data.num;
          if (3 - num > 0) {
            this.rules.paypwd.$setError(this.$t('common.IncorrectAssetPassTip', {
              num: 3 - num
            }));
          } else {
            this.rules.paypwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
          }
        } else if (rep.code === window.CODE.PAY_PWD_LOCK.CODE) {
          this.rules.paypwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
        } else {
          window.error(rep.msg)
        }
      }).fail((rep) => {
        window.error(rep.msg)
      })

    },
    //隐藏弹窗
    hide(data) {
      this.payPassWord = '';
      this.$emit('hide', data)
    }
  }
})